/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.utils;

import org.apache.commons.lang3.StringUtils;

import com.sigmatrix.h5.enums.ModuleCodeEnum;
import com.sigmatrix.h5.exception.ServiceException;

/**
 *@ClassName: ModuleCodeUtil
 *@Description: 获得各模块的编码规则
 *@author youjun
 *@date 2017年7月17日 下午4:22:29
 */
public class ModuleCodeUtil {
	
	/**数值部分不足时,填充的占位符*/
	public static final String FILL_PLACEHOLDER = "0";

	/**
	 *@Title: getModuleCode
	 *@Description: code码组成:类型(必有)+日期部分+其他组成部分+数值部分(必有)
	 *@return String
	 *@author youjun
	 *@date 2017年7月17日 下午4:23:48
	 *@param moduleCodeEnum
	 *@param otherPart code中的其他组成部分
	 *@param sequenceNumber 序号,一般同主键ID
	 *@return
	 */
	public static String getModuleCode(final ModuleCodeEnum moduleCodeEnum, final String otherPart, final int sequenceNumber) throws ServiceException {
		
		if(moduleCodeEnum!=null) {
			final StringBuilder moduleCode = new StringBuilder(moduleCodeEnum.getModuleCode());
			// 是否需要其他组成部分
			final boolean isQequiredOtherPart = moduleCodeEnum.getIsQequiredOtherPart();
			if(isQequiredOtherPart && StringUtils.isBlank(otherPart)) {
				final StringBuilder sb = new StringBuilder("当前模块:").append(moduleCodeEnum.getDesc());
				sb.append("的编码中其他组成部分必须有");
				throw new ServiceException(sb.toString());
			}
			final int dataPartType = moduleCodeEnum.getDataPartType();
			// 当前时间格式串
			final String currentDateStr = getCurrentDateStr(dataPartType);
			// 处理数值部分,共x位数字
			final int numericalDigits = moduleCodeEnum.getNumericalDigits();
			final String sequenceNumberStr = getSequenceNumberStr(sequenceNumber, numericalDigits);
			moduleCode.append(currentDateStr).append(otherPart).append(sequenceNumberStr);
			return moduleCode.toString();
		}
		return "";
	}

	/**
	 *@Title: getSequenceNumberStr
	 *@Description: 组装code的数字部分
	 *@return String
	 *@author youjun
	 *@date 2017年7月17日 下午4:49:03
	 *@param sequenceNumber 序号,一般同主键ID
	 *@param numericalDigits 位数
	 *@return
	 */
	private static String getSequenceNumberStr(final int sequenceNumber, final int numericalDigits) throws ServiceException {

		// 当前序号的位数
		final int count = (sequenceNumber+"").length();
		if(count > numericalDigits) {
			final StringBuilder sb = new StringBuilder("生成模块编码传递的序号:").append(sequenceNumber);
			sb.append("不能大于规定的位数:").append(numericalDigits).append("位数字");
			throw new ServiceException(sb.toString());
		}
		if(count < numericalDigits) {
			final int differValue = numericalDigits - count; // 差值
			final StringBuilder polishing = new StringBuilder(); // 补齐部分
			for(int i=0;i<differValue;i++) {
				polishing.append(FILL_PLACEHOLDER);
			}
			polishing.append(sequenceNumber);
			return polishing.toString();
		}
		// 相等时原样返回
		return sequenceNumber+"";
	}

	/**
	 *@Title: getCurrentDateStr
	 *@Description: 获得当前的操作时间
	 *@return String
	 *@author youjun
	 *@date 2017年7月17日 下午4:36:46
	 *@param dataPartType
	 *@return
	 */
	private static String getCurrentDateStr(final int dataPartType) {
		
		String currentDateStr = "";
		switch (dataPartType) {
		case Cons.DATA_PART_TYPE_NO:
			break;
		case Cons.DATA_PART_TYPE_YYMMMDD:
			currentDateStr = DateUtil.getCurrentDateStr(DateUtil.YYMMDD);
			break;
		case Cons.DATA_PART_TYPE_YYYYMMMDD:
			currentDateStr = DateUtil.getCurrentDateStr(DateUtil.YYYYMMDD);
			break;
		default:
			break;
		}
		return currentDateStr;
	}

}
